package org.facyt.commons.view.interfaces.window;

import org.apache.commons.lang3.StringUtils;
import org.facyt.commons.util.VaadinUtil;

import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.BaseTheme;

public class WindowMessage extends Window {

	private static final long serialVersionUID = 7424876443345108823L;

	public static void showMessage(WindowInfo info) {
		Window window = createWindowMessage(info);
		info.getApplication().getMainWindow().addWindow(window);
	}

	public static Window createWindowMessage(WindowInfo info) {
		String title = VaadinUtil.getProperties(info.getTitle());
		if (title == null) {
			title = info.getTitle();
		}
		Window window = new Window(title);

		GridLayout gridLayout = new GridLayout(2, 2);
		gridLayout.setMargin(true);
		gridLayout.setSpacing(true);
		gridLayout.setSizeFull();
		gridLayout.setColumnExpandRatio(0, 0.2f);
		gridLayout.setColumnExpandRatio(1, 0.8f);

		Label label = new Label();
		label.setIcon(info.getIcon());
		gridLayout.addComponent(label);
		gridLayout.setComponentAlignment(label, Alignment.TOP_CENTER);

		Component component = createMessageComponent(info);
		gridLayout.addComponent(component);
		gridLayout.setComponentAlignment(component, Alignment.MIDDLE_CENTER);

		Button button = new Button(VaadinUtil.getProperties(info.getButtonLabel()));
		button.addListener(new ClickListener() {

			@Override
			public void buttonClick(ClickEvent event) {
				Window owner = event.getButton().getWindow();
				owner.getParent().removeWindow(owner);
			}
		});
		gridLayout.addComponent(button, 0, 1, 1, 1);
		gridLayout.setComponentAlignment(button, Alignment.MIDDLE_CENTER);

		window.setContent(gridLayout);
		window.setWidth(getWidth(info.getMessage()), UNITS_PIXELS);
		window.setCloseShortcut(KeyCode.ESCAPE);
		window.setModal(true);
		return window;
	}

	protected static Component createMessageComponent(final WindowInfo info) {
		VerticalLayout layout = new VerticalLayout();
		layout.setMargin(false);
		Label label = new Label(info.getMessage());
		layout.addComponent(label);

		if (StringUtils.isNotBlank(info.getDetail())) {
			Button button = new Button(VaadinUtil.getProperties(info.getButtonDetail()));
			button.setStyleName(BaseTheme.BUTTON_LINK);
			layout.addComponent(button);
			layout.setComponentAlignment(button, Alignment.BOTTOM_RIGHT);
			button.addListener(new ClickListener() {

				@Override
				public void buttonClick(ClickEvent event) {
					Window window = createDetailWindow(info);
					event.getButton().getApplication().getMainWindow().addWindow(window);
				}
			});
		}

		return layout;
	}

	protected static Window createDetailWindow(WindowInfo info) {
		Window window = new Window();
		window.setWidth(650, UNITS_PIXELS);

		TextArea textArea = new TextArea();
		textArea.setValue(info.getDetail());
		textArea.setReadOnly(true);
		textArea.setSizeFull();
		textArea.setRows(30);
		window.addComponent(textArea);

		return window;
	}

	/**
	 * Formatea un mensaje para limitar la cantidad de letras por linea, agregando saltos de linea (separator)
	 * 
	 * @param message
	 * @param length
	 * @param separator
	 * @return
	 */
	public static int getWidth(String message) {

		int width = 0;
		int length = message == null ? 200 : message.length();
		if (length < 200) {
			width = 350;
		} else if (length < 400) {
			width = 500;
		} else {
			width = 550;
		}

		return width;
	}
}
